//
//  RegisterViewController.m
//  LOU
//
//  Created by iMacOne on 15/11/6.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "RegisterViewController.h"
#import "Size.h"
#import "ToolUtil.h"
#import "LeafNotification.h"
#import <SMS_SDK/SMSSDK.h>
@interface RegisterViewController (){
    NSInteger i;
    NSTimer *timer;
}


@property (nonatomic, strong)ToolUtil * toolUtilProvinces;
@property (nonatomic, strong)ToolUtil * toolUtilCity;

@property (nonatomic, strong)NSMutableArray * provincesArray;
@property (nonatomic, strong)NSMutableArray * citysArray;

@property (nonatomic, strong)UITextField * phoneNumberTF;
@property (nonatomic, strong)UITextField * codeTF;
@property (nonatomic, strong)UILabel * cityLabel;
@property (nonatomic, strong)UITextField * nicknameTF;
@property (nonatomic, strong)UIButton * sendCode;
@property (nonatomic, strong)UIButton * registerButton;
@property (nonatomic, strong)UILabel * textLabel;
@property (nonatomic, strong)ToolUtil * tool;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    [ToolUtil createLine:margin_64 superView:self.view lift:0 width:WindowWidth hight:0.5 color:[UIColor grayColor]];

    [self createTitle:@"登录" midTitle:@"注册"];
    [self creatView];
   
    
    
    
    // Do any additional setup after loading the view.
}
- (void)creatView
{
    self.phoneNumberTF = [[UITextField alloc] initWithFrame:CGRectMake((WindowWidth - margin_250)/ 2, margin_48 + 64, margin_250,margin_34)];
    //        _phoneNumberTF.borderStyle = UITextBorderStyleRoundedRect;
    _phoneNumberTF.placeholder = @" 请输入手机号码";
    _phoneNumberTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneNumberTF.backgroundColor = [UIColor whiteColor];
    _phoneNumberTF.inputAccessoryView = nil;
    _phoneNumberTF.font = FONT(14);
    _phoneNumberTF.delegate = self;
    [self.view addSubview:_phoneNumberTF];
    float y1 = [ToolUtil createLine:CGRectGetMaxY(_phoneNumberTF.frame) + 1 superView:self.view lift:_phoneNumberTF.frame.origin.x width:_phoneNumberTF.frame.size.width hight:0.5 color:[UIColor grayColor]];
    
    self.codeTF = [[UITextField alloc] initWithFrame:CGRectMake(_phoneNumberTF.frame.origin.x, y1 + margin_10, margin_160,margin_34)];
    //        _phoneNumberTF.borderStyle = UITextBorderStyleRoundedRect;
    _codeTF.placeholder = @" 请输入验证码";
    _codeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeTF.backgroundColor = [UIColor whiteColor];
    _codeTF.inputAccessoryView = nil;
    _codeTF.font = FONT(14);
    _codeTF.delegate = self;
    [self.view addSubview:_codeTF];
    
     float y2 = [ToolUtil createLine:CGRectGetMaxY(_phoneNumberTF.frame) + 45  superView:self.view lift:_codeTF.frame.origin.x width:_phoneNumberTF.frame.size.width hight:0.5 color:[UIColor grayColor]];
    
    self.sendCode = [UIButton buttonWithType:UIButtonTypeCustom];
    _sendCode.frame = CGRectMake(CGRectGetMaxX(_codeTF.frame), _codeTF.frame.origin.y - margin_5, margin_90, margin_34);
    _sendCode.backgroundColor = [UIColor blackColor];
    [_sendCode setTitle:@"发送验证码" forState:UIControlStateNormal];
    
    [_sendCode setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_sendCode addTarget:self action:@selector(pressverify) forControlEvents:UIControlEventTouchUpInside];
    _sendCode.tag = 300;
    _sendCode.titleLabel.font = FONT(14);
    [self.view addSubview:_sendCode];
    
    self.nicknameTF = [[UITextField alloc] initWithFrame:CGRectMake(_phoneNumberTF.frame.origin.x, y2 + margin_10, margin_160,margin_34)];
    //        _phoneNumberTF.borderStyle = UITextBorderStyleRoundedRect;
    _nicknameTF.placeholder = @" 请输入昵称";
    _nicknameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _nicknameTF.backgroundColor = [UIColor whiteColor];
    _nicknameTF.inputAccessoryView = nil;
    _nicknameTF.font = FONT(14);
    _nicknameTF.delegate = self;
    [self.view addSubview:_nicknameTF];
    
   float y3 =  [ToolUtil createLine:CGRectGetMaxY(_phoneNumberTF.frame) + margin_90  superView:self.view lift:_codeTF.frame.origin.x width:_phoneNumberTF.frame.size.width hight:0.5 color:[UIColor grayColor]];
    
    self.cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(_phoneNumberTF.frame.origin.x, y3 + margin_10, margin_250,margin_34)];
    //        _phoneNumberTF.borderStyle = UITextBorderStyleRoundedRect;
    _cityLabel.text = @" 请选择地区";
//    _cityTF.clearButtonMode = UITextFieldViewModeWhileEditing;
//    _cityTF.backgroundColor = [UIColor whiteColor];
//    _cityTF.inputAccessoryView = nil;
    _cityLabel.font = FONT(14);
//    _cityTF.delegate = self;
    _cityLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseArea)];
    [_cityLabel addGestureRecognizer:tap];
    [self.view addSubview:_cityLabel];
    
    [ToolUtil createLine:CGRectGetMaxY(_phoneNumberTF.frame) + margin_140  superView:self.view lift:_phoneNumberTF.frame.origin.x width:_phoneNumberTF.frame.size.width hight:0.5 color:[UIColor grayColor]];

    
    self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerButton.frame = CGRectMake((WindowWidth - margin_140) / 2, CGRectGetMaxY(_cityLabel.frame) + margin_34, margin_140, margin_34);
    _registerButton.backgroundColor = [UIColor blackColor];
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _registerButton.tag = 301;
    [_registerButton addTarget:self action:@selector(doClickButton:) forControlEvents:UIControlEventTouchUpInside];
    _registerButton.titleLabel.font = FONT(14);
    [self.view addSubview:_registerButton];
    
}
- (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}


- (void)pressverify{
    
    [[[ToolUtil alloc] init] pressverify:_phoneNumberTF.text control:self button:_sendCode];
//    if([ToolUtil isPhoneNumber: _phoneNumberTF.text]){
//        i = 60;
//        timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
//                [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_phoneNumberTF.text zone:@"+86" customIdentifier:@"吾诺瀚卓" result:^(NSError *error) {
//                    if (!error) {
//                        
//                        NSLog(@"info %@", error.userInfo);
//                    }else{
//                        NSLog(@"%@",error.userInfo[@"getVerificationCode"]);
//                    }
//                        
////           self.codeTF.text = ;
//        }];
//        
//    }else {
//        [LeafNotification showInController:self withText:@"请输入正确的手机号"];
//        
//    }
}
//
//
//- (void)updateTimer:(NSTimer *)sender{
//    
//    
//        [_sendCode setTitle:[NSString stringWithFormat:@"%ld秒",i] forState:UIControlStateNormal];
//        _sendCode.userInteractionEnabled = NO;
//    i--;
//    if (i == 0) {
//        _sendCode.userInteractionEnabled = YES;
//        [timer invalidate];
//        [_sendCode setTitle:[NSString stringWithFormat:@"发送验证码"] forState:UIControlStateNormal];
//    }
//    
//    NSLog(@"--------");
//    
//    
//    
//}

- (void)chooseArea
{
    
    
    
//    self.provincesArray = [NSMutableArray arrayWithObjects:@"浙江", @"广东",@"海南",@"江苏",@"上海",@"北京",@"福建",@"广西",@"湖南",@"湖北",nil];
//    self.toolUtilProvinces = [[ToolUtil alloc] init];
//    self.toolUtilProvinces.commboxArray = [NSMutableArray arrayWithArray:@[@"选择省份"]];
//    for (int j = 0; j < self.provincesArray.count; j++) {
//        [self.toolUtilProvinces.commboxArray addObject:self.provincesArray[j]];
//    }
//    
//    float txt_h = title_height * 0.9, txt_w = WindowWidth / 2 - 0.5;
//    CGRect frame = CGRectMake(0, CGRectGetMaxY(self.cityLabel.frame), txt_w, txt_h);
//    
//    float line_y = CGRectGetMaxY(frame), panel_h = WindowHeight - line_y - 6;
//    CGRect panelFrame = CGRectMake(2, line_y, txt_w - 4, panel_h);
//    [self.toolUtilProvinces createCommbox:@"" _rect:frame maxLength:0 control:self view:self.view callback:^(NSMutableDictionary * dic) {
//        
//    } text:@"选择省份" panelFrame:panelFrame isInput:NO align:NSTextAlignmentRight];
    
}
- (void)doClickButton:(UIButton *)btn
{
//    [self.tool prompt:@"haha" view:self.view];
    [self prompt:@"hhkjkj"];
    if ([ToolUtil isPhoneNumber:_phoneNumberTF.text] && [ToolUtil isBlank:_codeTF.text]) {
        [SMSSDK commitVerificationCode:_codeTF.text phoneNumber:_phoneNumberTF.text zone:@"86" result:^(NSError *error) {
            if (!error) {
                NSLog(@"验证成功");
            }else {
                NSLog(@"验证失败");
            }
        }];
    }
    
    
    
    NSLog(@"注册");
}
- (void)doBack:(UIButton*)btnBack{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = YES;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
